package com.chnnelsoft.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 根据生产的环境来判断，如果是生产环境那么就使用swagger
     *  发布环境就不使用swagger
     */
    //如果多人一起开发，就需要多个Docket
    @Bean
    public Docket docket1(Environment environment){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(Environment environment){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(Environment environment){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    //配置了swagger的Doket的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //获取当前运行的环境 dev、pro
        //String[] activeProfiles = environment.getActiveProfiles();
        Profiles profiles = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(apiInfo())
                .groupName("军费")
              .enable(flag)//是否启用swagger，如果为false，则swagger不能在浏览器中访问
              .select()
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
              .apis(RequestHandlerSelectors.basePackage("com.chnnelsoft.controller"))
              //过滤，只扫描路径带有/zhao的
              //.paths(PathSelectors.ant("/zhao"))
              .build();//
    }
    //配置swagger文档信息=apiInfo
    private ApiInfo apiInfo(){
        Contact  contact = new Contact("fei", "http://10.210.231.111.com", "396402252");
        return new ApiInfo(
                "SwaggerApI文档",
                "你好帅哥",
                "1.0",
                "http://10.210.231.111.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }



}
